package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.QuestionDao;
import com.itheima.domain.Question;
import com.itheima.service.QuestionService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionDao questionDao;

    @Override
    public PageInfo<Question> findByPage(int currentPage, int pageSize) {
        //3.1 设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //3.2 查询所有
        List<Question> list = questionDao.findAll();
        //3.3 封装分页结果
        return new PageInfo<>(list);
    }

    /**
     * 保存信息
     * @param question
     */
    @Override
    public void save(Question question) {
        //生成唯一id
        String uuid = UUID.randomUUID().toString().replace("-", "");
        question.setId(uuid);
        //3 执行操作
        questionDao.save(question);
    }

    /**
     * 根据id查询信息的方法
     * @param id
     * @return
     */
    @Override
    public Question findById(String id) {
        return questionDao.findById(id);
    }
    /**
     * 修改数据的方法
     */
    @Override
    public void update(Question question) {
        questionDao.update(question);
    }

    /**
     * 批量删除信息的方法
     * @param ids
     */
    @Override
    public void delete(String[] ids) {
        Stream.of(ids).forEach(id->questionDao.delete(id));
    }

    /**
     * 根据查询所有信息的方法
     * @return
     */
    @Override
    public List<Question> findAll() {
        return questionDao.findAll();
    }

    /**
     * 题目报表导出
     * @param outputStream
     */
    @Override
    public void toExport(OutputStream outputStream) throws IOException {
        //获取模版文件的路径
        String path = this.getClass().getClassLoader().getResource("question_template.xlsx").getPath();
        //1 创建WorkBook对象，读取模版excel文件
        Workbook workbook=new XSSFWorkbook(path);
        //2 获取sheet工作表对象
        Sheet sheet = workbook.getSheetAt(0);
        //3 调用findAll方法，查询所有问题集合List<question>
        List<Question> questionList = findAll();
        //4 List<Question>转换成List<String[]> ，String[]中保存的是Question的各个属性值
        List<String[]> fileds = questionList.stream().map(question -> {
            //创建字符串类型数组，存储question的属性值
            String[] fields = new String[12];
            //将question的属性值保存到数组中
            fields[0] = question.getId();
            fields[1] = question.getCompanyId();
            fields[2] = question.getCatalogId();
            fields[3] = question.getRemark();
            fields[4] = question.getSubject();
            fields[5] = question.getPicture();
            fields[6] = question.getAnalysis();
            fields[7] = question.getType();
            fields[8] = question.getDifficulty();
            fields[9] = question.getIsClassic();
            fields[10] = question.getState();
            fields[11] = question.getReviewStatus();
            return fields;
        }).collect(Collectors.toList());
        //定义通用的样式
        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        //5 遍历集合，向单元格中写入数据
        //定义起始行索引
        int rowIndex=3;
        for (String[] arr : fileds) {  //每遍历一次就多一行
            //创建行
            Row row = sheet.createRow(rowIndex++);
            //遍历arr数组，向单元格中写入数据
            //定义起始列
            int cellIndex=1;
            for (String data : arr) { //每遍历一次，这一行就多一个单元格
                //创建列
                Cell cell = row.createCell(cellIndex++);
                //写入数据
                cell.setCellValue(data);
                //设置样式
                cell.setCellStyle(style);
            }
        }
        //6 响应给客户端，释放资源
        workbook.write(outputStream);
        workbook.close();
    }
}
